package com.chaos.gateway.config.properties;


import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * @author Jacky
 * @date 2020/11/4 11:27
 */
@Data
@Component
@ConfigurationProperties("xss")
public class XssProperties {

    private List<XssWhiteUrl> whiteUrls;

    @Data
    @Validated
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    private static class XssWhiteUrl {

        @NotEmpty
        private String url;

        @NotEmpty
        private String method;
    }

    public boolean white(String url, String method) {
        return whiteUrls != null && whiteUrls.contains(XssWhiteUrl.builder().url(url).method(method).build());
    }
}
